//
//  Head.m
//  TapEnglish
//
//  Created by 이선동 on 11. 4. 13..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Head.h"
#import "TapEnglishAppDelegate.h"

@implementation Head

NSString *currentEngWord;

- (TapEnglishAppDelegate *)appDelegate {
    return [[UIApplication sharedApplication] delegate];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [labelCaption release];
    [labelPinyin release];
    [segmentFlag release];
    [button_01 release];
    [button_02 release];
    [button_03 release];
    [button_04 release];
    [button_05 release];
    [button_11 release];
    [button_12 release];
    [button_13 release];
    [button_14 release];
    [button_15 release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [labelCaption release];
    labelCaption = nil;
    [labelPinyin release];
    labelPinyin = nil;
    [segmentFlag release];
    segmentFlag = nil;
    [button_01 release];
    button_01 = nil;
    [button_02 release];
    button_02 = nil;
    [button_03 release];
    button_03 = nil;
    [button_04 release];
    button_04 = nil;
    [button_05 release];
    button_05 = nil;
    [button_11 release];
    button_11 = nil;
    [button_12 release];
    button_12 = nil;
    [button_13 release];
    button_13 = nil;
    [button_14 release];
    button_14 = nil;
    [button_15 release];
    button_15 = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewWillAppear:(BOOL)animated {
    currentEngWord = @"head";
    if (segmentFlag.selectedSegmentIndex == 0) {
        [self working:currentEngWord];
    } 
    [segmentFlag setSelectedSegmentIndex:[[self appDelegate].nationCode intValue]];
}

- (IBAction)goHome:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)speakerTapped:(id)sender {
    [self working:currentEngWord];
}

- (IBAction)button_01_tapped_hair:(id)sender {
    [self working:@"hair"];
    [self buttonColorReset];
    [button_01 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_02_tapped_eyebrow:(id)sender {
    [self working:@"eyebrow"];
    [self buttonColorReset];
    [button_02 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_03_tapped_ear:(id)sender {
    [self working:@"ear"];
    [self buttonColorReset];
    [button_03 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_04_tapped_cheek:(id)sender {
    [self working:@"cheek"];
    [self buttonColorReset];
    [button_04 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_05_tapped_chin:(id)sender {
    [self working:@"chin"];
    [self buttonColorReset];
    [button_05 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_11_tapped_head:(id)sender {
    [self working:@"head"];
    [self buttonColorReset];
    [button_11 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_12_tapped_eye:(id)sender {
    [self working:@"eye"];
    [self buttonColorReset];
    [button_12 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_13_tapped_nose:(id)sender {
    [self working:@"nose"];
    [self buttonColorReset];
    [button_13 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_14_tapped_mouth:(id)sender {
    [self working:@"mouth"];
    [self buttonColorReset];
    [button_14 setBackgroundColor:[UIColor blueColor]];
}

- (IBAction)button_15_tapped_neck:(id)sender {
    [self working:@"neck"];
    [self buttonColorReset];
    [button_15 setBackgroundColor:[UIColor blueColor]];
}

- (void) working:(NSString *)engWord {
    currentEngWord=engWord;

    NSArray *imsiArray = [[NSArray alloc] initWithArray:[[self appDelegate].vocaGroup valueForKey:engWord]];
    
    switch ([[self appDelegate].nationCode intValue]) {
        case 0: //영어
            labelCaption.text=[imsiArray objectAtIndex:0];
            labelPinyin.text=@"";
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"ENG_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 1: //중국어
            labelCaption.text=[imsiArray objectAtIndex:1];
            labelPinyin.text=[imsiArray objectAtIndex:2];
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"CHA_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 2: //일본어
            labelCaption.text=[imsiArray objectAtIndex:3];
            labelPinyin.text=[imsiArray objectAtIndex:4];
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"JPN_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        case 3: //한국어
            labelCaption.text=[imsiArray objectAtIndex:5];
            labelPinyin.text=@"";
            [[self appDelegate] soundPlay:[NSString stringWithFormat:@"KOR_%@.aiff", [imsiArray objectAtIndex:0]]];
            break;
        default:
            NSLog(@"Unknow nationCode");
            break;
    }
    [imsiArray release];
}

- (IBAction)segmentNation_tapped:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    [self appDelegate].nationCode = [NSNumber numberWithInt:segmentedControl.selectedSegmentIndex];
    [self working:currentEngWord];
}

- (void) buttonColorReset {
    [button_01 setBackgroundColor:[UIColor whiteColor]];
    [button_02 setBackgroundColor:[UIColor whiteColor]];
    [button_03 setBackgroundColor:[UIColor whiteColor]];
    [button_04 setBackgroundColor:[UIColor whiteColor]];
    [button_05 setBackgroundColor:[UIColor whiteColor]];
    [button_11 setBackgroundColor:[UIColor whiteColor]];
    [button_12 setBackgroundColor:[UIColor whiteColor]];
    [button_13 setBackgroundColor:[UIColor whiteColor]];
    [button_14 setBackgroundColor:[UIColor whiteColor]];
    [button_15 setBackgroundColor:[UIColor whiteColor]];
}

@end
